package com.eagle.common.core.support;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wcy
 * @Description 统一返回类型<br>
 * Project: empire <br>
 *
 * @date Created in 2018/7/16 17:12 <br>
 */
@Data
public class EsPage<T> extends ArrayList<T> {

    /**
     * 分页总条数
     */
    private long total;
    /**
     * 当前页数
     */
    private int current;
    /**
     * 当前页显示条
     */
    private int size;

    public EsPage() {
    }

    public EsPage(List<T> data) {
        super(data);
    }

    @SuppressWarnings("unchecked")
    public static <T> EsPage<List<T>> success(List<T> data, PageModel page, Long count) {
        return Builder.builder()
                .withCurrent(page.getPageNum())
                .withTotal(count)
                .withSize(page.getPageSize())
                .build(data);
    }

    public static final class Builder<T> {

        private long total;

        private int current;

        private int size;

        private Builder() {
        }

        public static Builder builder() {
            return new Builder();
        }

        public Builder withCurrent(int current) {
            this.current = current;
            return this;
        }

        public Builder withSize(int size) {
            this.size = size;
            return this;
        }

        public Builder withTotal(long total) {
            this.total = total;
            return this;
        }

        public EsPage build(List<T> data) {
            EsPage esPage = new EsPage<>(data);
            esPage.setCurrent(current);
            esPage.setSize(size);
            esPage.setTotal(total);
            return esPage;
        }
    }


}
